package com.qj.yantaishan.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.qj.yantaishan.common.core.controller.BaseController;
import com.qj.yantaishan.common.core.domain.R;
import com.qj.yantaishan.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 【请填写功能名称】Controller
 *
 * @author qihe
 * @date 2022-07-22
 */

@RestController
@RequestMapping("/yantaishan/Admin")
public class AdminController extends BaseController {
    private String prefix = "yantaishan/Admin";

    @Autowired
    private IAdminService adminService;

    @PostMapping("login")
    public R login(@RequestBody Map<String, String> body) {
        String account = body.get("account");
        String pwd = body.get("pwd");
        Map<String, Object> result = adminService.login(account, pwd);
        if (result.get("token").equals("0")) {
            return R.fail("登录失败");
        }

        return R.ok(result, "登录成功");
    }

    @PutMapping("logout")
    public R logout(HttpServletRequest request) {
        String token = request.getHeader("token");
        StpUtil.logoutByTokenValue(token);

        return R.ok("退出登录成功~");
    }


    @GetMapping("list")
    public R getAdminList(@RequestParam(required = false, defaultValue = "1") Integer currentPage,
                          @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Map<String, Object> result = adminService.selectManageList(currentPage, pageSize);
        return R.ok(result);
    }

    @PostMapping("manage")
    public R addStoreManage(Integer uid) {
        int flag = adminService.addStoreManage(uid);
        if (flag == 404) return R.fail("用户不存在");
        if (flag == 501) return R.fail("管理员已存在");
        return R.ok("添加成功");
    }

    @DeleteMapping()
    public R deleteManage(Integer uid) {
        adminService.deleteManage(uid);
        return R.ok("删除成功");
    }


}
